package com.iptv.grape.jss.impls.listener;

import android.webkit.WebView;

import com.iptv.grape.jss.impls.PlayPro;
import com.iptv.grape.jss.mgr.CallJsMgr;
import com.iptv.grape.util.AppLog;
import com.iptv.grape.video.IPlayerListener;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/***
 * 视频监听
 */
public class PlayEventListener implements IPlayerListener {
    private Map<String,PlayPro> proListeners = new ConcurrentHashMap<String,PlayPro>();
    private WebView webView;
    public  PlayEventListener(WebView webView){
        this.webView = webView;
    }
    public PlayEventListener(){

    }

    // 添加事件监听者
    public void addProListener(String tag,PlayPro proListener){
        proListeners.put(tag,proListener);
    }
    // 移除事件监听者
    public void removeProListener(String tag){
        if(!proListeners.isEmpty()){
            proListeners.remove(tag);
        }
    }

    @Override
    public void onPlaying() {

    }

    @Override
    public void onPlayPause() {

    }

    @Override
    public void onStop(int speed) {
        if(webView==null)
            CallJsMgr.sendPlayEnd(webView);
    }

    @Override
    public void onError() {
        if(webView==null)
            CallJsMgr.sendPlayError(webView);
    }

    @Override
    public void onProgress(int speed) {
        AppLog.getCurLog().e("show===>pro");
        if(proListeners!=null&& proListeners.size()>0){
            AppLog.getCurLog().e("size==="+proListeners.size());
            for (Map.Entry<String,PlayPro> entry:proListeners.entrySet()) {
                entry.getValue().pro(speed);
            }
        }
    }

    @Override
    public void onIntoFullScreen() {

    }

    @Override
    public void onQuitFullScreen() {

    }

    @Override
    public void playCompletion() {
        if(proListeners!=null&& proListeners.size()>0){
            AppLog.getCurLog().e("size==="+proListeners.size());
            for (Map.Entry<String,PlayPro> entry:proListeners.entrySet()) {
                entry.getValue().completion();
            }
        }
    }
}
